package com.Web.Servlet;

import com.Web.Person.User;
import com.Web.Service.IUserService;
import com.Web.Service.Impl.UserServiceImpl;
import com.Web.util.JSONUtil;
import com.Web.util.PageInfo;
import com.Web.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {

    private IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet.service");
        //req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if(method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "login":
                login(req,resp);
                break;
            case "logout":
                logout(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "selectUserAddPage":
                selectUserAddPage(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "toUpdate":
                toUpdate(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        User user = new User(Integer.parseInt(id),name, password);
        userService.update(user);
        resp.sendRedirect("/user");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.toUpdate");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        User user = userService.selectById(id);
        req.setAttribute("user",user);
        req.getRequestDispatcher("/user_update.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.add");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        User user = new User(name,password);
        userService.add(user);
        resp.sendRedirect("/user");
    }

    private void selectUserAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.selectUserAddPage");
        req.getRequestDispatcher("/user_add.jsp").forward(req,resp);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("UserServlet.deleteById");
        String ids = req.getParameter("id");
        int id = Integer.parseInt(ids);
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if(id == user.getId()) {
            req.getRequestDispatcher("/delete_fail.jsp").forward(req,resp);
            return;
        }
        userService.deleteById(id);
        resp.sendRedirect("/user");
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.selectByPage");
        String pageNo = req.getParameter("pageNo");
        if(pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        String pageSize = req.getParameter("pageSize");
        if(pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        PageInfo<User> pageInfo = userService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher("/user_list.jsp").forward(req,resp);
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.logout");
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        resp.sendRedirect("/login.jsp");
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.login");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        HttpSession session = req.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if(!codeInSession.equalsIgnoreCase(code)) {
            JSONUtil.toJSON(resp, Result.error("验证码错误"));
            return;
        }

        User user = userService.login(name, password);
        if(user != null) {
            session.setAttribute("user",user);
            //resp.sendRedirect("/");
            JSONUtil.toJSON(resp,Result.ok("登录成功"));
        } else {
            //resp.sendRedirect("/fail.jsp");
            JSONUtil.toJSON(resp,Result.error("登录失败"));
        }
    }
}
